package com.nx.mr.wordcount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @Author: Qingwr
 * @Date: 2022/8/13 21:24
 * @Description maptask之后给 maptask 的结果进行局部汇总，以减轻 reducetask 的计算负载，减少网络传输
 */

public class MyCombiner extends Reducer<Text, LongWritable, Text, LongWritable> {

    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws InterruptedException, IOException {
        //1、定义一个变量
        long count = 0;

        //2、迭代
        for (LongWritable value : values) {
            count += value.get();
        }

        //3、写入到上下文
        context.write(key, new LongWritable(count));
    }
}
